package wonder.tools;

import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;

/**
 * <p>ParameterHelper class.</p>
 *
 * @author u211354
 * @version $Id: $
 */
public class ParameterHelper {
	private static final Logger logger = LoggerHelper.getLogger();
	private final static ObjectMapper jsonMapper = new ObjectMapper();

	/** Constant <code>CLASS_NAME="className"</code> */
	public static final String CLASS_NAME = "className";
	/** Constant <code>FILE="file"</code> */
	public static final String FILE = "file";
	/** Constant <code>RESOURCE="resource"</code> */
	public static final String RESOURCE = "resource";

	/**
	 * <p>newInstance</p>
	 *
	 * @param params a {@link java.lang.String} object.
	 * @return a {@link wonder.tools.IParametrable} object.
	 * @throws java.lang.Exception if any.
	 */
	@SuppressWarnings("unchecked")
	public static IParametrable newInstance(String params) throws Exception {
		HashMap<String, Object> map = null;
		try {
			map = jsonMapper.readValue(params, HashMap.class);
		} catch (Exception e) {
			logger.error("Error parsing {}",
					params + "\n" + LoggerHelper.getStackTrace(e));
			throw e;
		}
		return newInstance(map);
	}

	/**
	 * <p>newInstance</p>
	 *
	 * @param map a {@link java.util.Map} object.
	 * @return a {@link wonder.tools.IParametrable} object.
	 * @throws java.lang.Exception if any.
	 */
	public static IParametrable newInstance(Map<String, Object> map)
			throws Exception {
		IParametrable rc = null;

		String className = (String) map.get(CLASS_NAME);
		if (className != null) {
			rc = (IParametrable) Class.forName(className).newInstance();
			rc.parameter(map);
		}

		return rc;
	}

	/**
	 * <p>newInstance</p>
	 *
	 * @param cl a {@link java.lang.ClassLoader} object.
	 * @param params a {@link java.lang.String} object.
	 * @return a {@link wonder.tools.IParametrable} object.
	 * @throws java.lang.Exception if any.
	 */
	@SuppressWarnings("unchecked")
	public static IParametrable newInstance(ClassLoader cl, String params)
			throws Exception {
		HashMap<String, Object> map = null;
		try {
			map = jsonMapper.readValue(params, HashMap.class);
		} catch (Exception e) {
			logger.error("Error parsing {}",
					params + "\n" + LoggerHelper.getStackTrace(e));
			throw e;
		}
		return newInstance(cl, map);
	}

	/**
	 * <p>newInstance</p>
	 *
	 * @param cl a {@link java.lang.ClassLoader} object.
	 * @param map a {@link java.util.Map} object.
	 * @return a {@link wonder.tools.IParametrable} object.
	 * @throws java.lang.Exception if any.
	 */
	public static IParametrable newInstance(ClassLoader cl,
			Map<String, Object> map) throws Exception {
		IParametrable rc = null;

		String className = (String) map.get(CLASS_NAME);
		if (className != null) {
			rc = (IParametrable) cl.loadClass(className).newInstance();
			rc.parameter(map);
		}

		return rc;
	}
}
